﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Database.Entity;
using ASBCC.Database.Service;
using ASBCC.Workflow.K2Manager;
using ASBCC.Business;
using System.Data;

namespace BEGDC.CC.Web.Workflow.Admin
{
    public partial class FlowProcessLogEdit : System.Web.UI.Page
    {
        private Guid ID
        {
            get
            {
                string id = Request.QueryString["ID"];
                if (string.IsNullOrEmpty(id)) return Guid.Empty;
                return new Guid(id);
            }
        }

        private void InitControl()
        {
            //txtOwnerEn.Attributes["readonly"] = "true";
            //txtDelegateFromEn.Attributes["readonly"] = "true";
        }

        private void BindProcNameDropdownlist()
        {
            Flow_ProcessTableEntitySet set = Flow_ProcessManager.SelectAll();
            DataTable dt = null;
            if (set != null && set.Tables.Count > 0)
            {
                dt = set.Tables[0];
            }
            //if (dt != null)
            //{
            //    DataRow dr = dt.NewRow();
            //    dr["ProcName"] = "";
            //    dr["ProcDisplayEn"] = "";
            //    dt.Rows.InsertAt(dr, 0);
            //}

            DataView dv = new DataView(dt);
            dv.Sort = "ProcName";

            ddlProcName.DataTextField = "ProcDisplayEn";
            ddlProcName.DataValueField = "ProcName";
            ddlProcName.DataSource = dv;
            ddlProcName.DataBind();
        }

        private void BindActivityNameDropdownList()
        {
            string procName = ddlProcName.SelectedValue;
            if (string.IsNullOrEmpty(procName))
            {
                ddlActivityName.Items.Clear();
            }
            else
            {
                Flow_ProcessConfigTableEntitySet set = Flow_ProcessConfigManager.SelectByCondition(string.Format("ProcName='{0}'", procName));
                DataTable dt = set.Tables[0];
                DataRow dr = dt.NewRow();
                dr["ActivityName"] = string.Empty;
                dr["OrderIndex"] = -1;
                dt.Rows.Add(dr);
                DataView dv = new DataView(dt);
                dv.Sort = "OrderIndex";
                ddlActivityName.DataTextField = "ActivityName";
                ddlActivityName.DataValueField = "ActivityName";
                ddlActivityName.DataSource = dv;
                ddlActivityName.DataBind();
            }
        }

        private void BindEntity()
        {
            if (ID == Guid.Empty)
            {

            }
            else
            {
                Flow_ProcessLogTableEntity entity = Flow_ProcessLogManager.SelectByKeys(ID);
                lblIDValue.Text = ID.ToString();
                ddlProcName.SelectedValue = entity.ProcName + string.Empty; ;
                BindActivityNameDropdownList();
                ddlActivityName.SelectedValue = entity.ActivityName + string.Empty; ;
                ddlActionType.SelectedValue = entity.ActionType;
                hidOwnerID.Value = entity.OwnerID.ToString();
                //txtOwnerEn.Text = entity.OwnerEn + string.Empty;
                hidOwnerCn.Value = entity.OwnerCn + string.Empty;
                hidDelegateFromID.Value = entity.DelegateFromID.ToString();
                //txtDelegateFromEn.Text = entity.DelegateFromEn + string.Empty;
                hidDelegateFromCn.Value = entity.DelegateFromCn + string.Empty;

                SearchStaff1.EmployeeID = entity.OwnerID + string.Empty;
                SearchStaff1.EmployeeName = entity.OwnerEn + string.Empty;

                SearchStaff2.EmployeeID = entity.DelegateFromID + string.Empty;
                SearchStaff2.EmployeeName = entity.DelegateFromEn + string.Empty;

                txtApplicationNo.Text = entity.ApplicationNo + string.Empty;
                txtProcInstID.Text = entity.ProcInstID.ToString();
                txtCreatedDate.Text = entity.CreatedDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }

        private void SaveEntity()
        {
            Flow_ProcessLogTableEntity entity;
            if (ID == Guid.Empty)
            {
                entity = new Flow_ProcessLogTableEntity();
                entity.ProcessLogID = Guid.NewGuid();
            }
            else
            {
                entity = Flow_ProcessLogManager.SelectByKeys(ID);
            }
            entity.ProcName = ddlProcName.SelectedValue;
            entity.ActivityName = ddlActivityName.SelectedValue;
            entity.ActionType = ddlActionType.SelectedValue;

            //wsq
            //entity.OwnerID = Convert.ToInt32(hidOwnerID.Value);
            //entity.OwnerEn = txtOwnerEn.Text;
            //entity.OwnerCn = hidOwnerCn.Value;

            EmployeeTableEntity ent1 = EmployeeManager.SelectByKeys(SearchStaff1.EmployeeID);
            if (ent1 != null)
            {
                entity.OwnerID = ent1.EmployeeID;
                entity.OwnerEn = ent1.NameEN;
                entity.OwnerCn = ent1.NameChinese;
            }

            int delegateFromId;

            //wsq
            //bool resDelegateFromId = int.TryParse(hidDelegateFromID.Value, out delegateFromId);
            //if (resDelegateFromId)
            //{
            //    entity.DelegateFromID = delegateFromId;
            //}
            //else
            //{
            //    entity.DelegateFromID = null;
            //}
            //entity.DelegateFromEn = txtDelegateFromEn.Text;
            //entity.DelegateFromCn = hidDelegateFromCn.Value;

            EmployeeTableEntity ent2 = EmployeeManager.SelectByKeys(SearchStaff2.EmployeeID);
            if (ent2 != null)
            {
                entity.DelegateFromID = ent2.EmployeeID;
                entity.DelegateFromEn = ent2.NameEN;
                entity.DelegateFromCn = ent2.NameChinese;
            }

            entity.ApplicationNo = txtApplicationNo.Text;
            entity.ProcInstID = Convert.ToInt32(txtProcInstID.Text);
            entity.CreatedDate = Convert.ToDateTime(txtCreatedDate.Text);

            if (ID == Guid.Empty)
            {
                Flow_ProcessLogManager.Insert(entity);
            }
            else
            {
                Flow_ProcessLogManager.Update(entity);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                BindProcNameDropdownlist();
                BindEntity();
            }
        }

        protected void ddlProcName_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindActivityNameDropdownList();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            DateTime tmpCreatedDate = new DateTime();
            if (!DateTime.TryParse(txtCreatedDate.Text, out tmpCreatedDate))
            {
                Page.RegisterClientScriptBlock("", "<script>alert('日期非法！');</script>");
                return;
            }
            SaveEntity();
            //this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", string.Format("alert('{0}');window.close();", "SaveSuccessfully"), true);
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", string.Format("alert('{0}');window.close();window.returnValue='reload';", "SaveSuccessfully"), true);
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("FlowProcessLog.aspx");
        }
    }
}
